<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>

        <!-- 主体内容 -->
        <el-tree :data="list" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children" @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>

      </el-card>
    </div>

    <!-- 弹窗 -->
    <el-dialog
      :title="isEdit ? '编辑部门' : '新增部门'"
      :visible.sync="showHide"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <deptDialog v-if="showHide" :id="curId" :is-edit="isEdit" :origin-list="originList" @success="hSuccess" />
    </el-dialog>

  </div>
</template>

<script>
import { getDepartments, delDepartment } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index'
import deptDialog from './deptDialog.vue'
export default {
  components: {
    deptDialog
  },
  data() {
    return {
      originList: [],
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }, // 数据
      showHide: false, // 弹窗显示与隐藏
      curId: '', // 当前选中部门的id
      isEdit: false // 是否处于编辑状态
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async loadDepartments() {
      const data = await getDepartments()
      this.originList = data.data.depts.map(item => {
        return {
          id: item.id,
          code: item.code,
          name: item.name,
          pid: item.pid
        }
      })
      this.list = tranListToTreeData(data.data.depts)
      console.log(this.list, '请求的数据')
    },
    // 点击添加子部门
    hAdd(id) {
      // 显示弹窗
      this.showHide = true
      // 选中部门id
      this.curId = id
      this.isEdit = false
    },
    // 点击编辑部门
    hEdit(id) {
      this.isEdit = true
      this.showHide = true
      this.curId = id
    },
    // 点击删除
    hDel(id) {
      this.$confirm('确认删除部门吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.doDel(id)
      }).catch(() => {

      })
    },
    async doDel(id) {
      await delDepartment(id)
      this.$message.success('删除成功')
      this.loadDepartments()
    },
    hSuccess() {
      this.showHide = false
      this.loadDepartments()
    }
  }
}
</script>
